import { defineStore } from "pinia";
import { useConfigure } from "./configure.js";
import { ref } from "vue";

export const useRoutes = defineStore("useRoutes", () => {
  const routesList = ref([])
  const allRoutes = ref({})
  const breadcrumb = ref([])
  const breadcrumbKeys = ref([])
  const navList = ref(new Map())
  const activeRoute = ref('')
  const cachedRoute = ref([])
  // 设置路由列表
  const setRoutesList = (routes) => {
    routesList.value = routes
  }
  // 设置当前的所有路由
  const setAllRoutes = (routes) => {
    routes.forEach((i) => {
      allRoutes.value[i.path] = i;
    });
  }
  // 设置需要缓存的路由
  const setCachedRoute = (name) => {
    if (!cachedRoute.value.includes(name)) {
      cachedRoute.value.push(name);
    }
  }
  // 设置面包屑
  const setBreadcrumb = (matched) => {
    breadcrumb.value = matched;
    breadcrumbKeys.value = [];
    matched.forEach((i) => {
      breadcrumbKeys.value.push(i.path);
    });
  }
  // 设置导航栏
  const setNavbar = (route) => {
    if (allRoutes.value[route]) {
      navList.value.set(route, allRoutes.value[route]);
      activeRoute.value = route;
      useConfigure().setDefaultActive(route);
    }
  }
  // 拖拽改变 navbar顺序
  const dragNavbar = (nav) => {
    if (nav.start !== nav.end && nav.start !== '/home' && nav.end !== '/home') {
      let list = [...navList.value],
        startIndex,
        startItem,
        endIndex;
      for (let i = 0; i < list.length; i++) {
        if (list[i][0] == nav.start) {
          startIndex = i
          startItem = list[i]
        }
        if (list[i][0] == nav.end) {
          endIndex = i
        }
        if (startIndex && endIndex) {
          list.splice(startIndex, 1)
          list.splice(endIndex, 0, startItem)
          navList.value = new Map(list)
          return
        }
      }
    }
  }
  // 设置navList列表
  const setNavList = (type) => {
    return new Promise((resolve) => {
      const navListKeys = [...navList.value].slice(1);
      switch (type) {
        case "else":
          for (let i = 0; i < navListKeys.length; i++) {
            if (navListKeys[i][0] !== activeRoute.value) {
              navList.value.delete(navListKeys[i][0]);
            }
          }
          break;
        case "left":
          for (let i = 0; i < navListKeys.length; i++) {
            if (navListKeys[i][0] == activeRoute.value) return;
            navList.value.delete(navListKeys[i][0]);
          }
          break;
        case "right":
          for (let i = navListKeys.length - 1; i >= 0; i--) {
            if (navListKeys[i][0] == activeRoute.value) return;
            navList.value.delete(navListKeys[i][0]);
          }
          break;
        case "all":
          for (let i = 0; i < navListKeys.length; i++) {
            navList.value.delete(navListKeys[i][0]);
          }
          resolve();
          break;
      }
    });
  }
  // 移除 navbar 中的某一项
  const deleteNavItem = (route) => {
    return new Promise((resolve) => {
      if (route == activeRoute.value) {
        let navList = [...navList.value],
          length = navList.length,
          r = "";
        for (let i = 0; i < length; i++) {
          if (navList[i][0] == route) {
            if (i == length - 1) {
              r = navList[i - 1];
            } else if (i < length - 1) {
              r = navList[i + 1];
            }
            navList.value.delete(route);
            resolve(r);
            return;
          }
        }
      } else {
        navList.value.delete(route);
      }
    });
  }
  return {
    routesList,
    allRoutes,
    breadcrumb,
    breadcrumbKeys,
    navList,
    activeRoute,
    cachedRoute,
    setRoutesList,
    setAllRoutes,
    setCachedRoute,
    setBreadcrumb,
    setNavbar,
    dragNavbar,
    setNavList,
    deleteNavItem
  }
});
